package com.rfid.afd.presenter;

import android.os.Handler;

import com.rfid.afd.activity.GatherActivity;
import com.rfid.afd.activity.listener.ModelListener;
import com.rfid.afd.activity.view.IGatherView;
import com.rfid.afd.model.GatherModel;
import com.rfid.util.Convert;
import com.rfid.util.LogUtils;
import com.rfid.util.RfidUitl;

import java.util.List;


public class GatherPresenter extends BasePresenter<IGatherView> implements ModelListener {

    GatherModel mModel;
    Handler mHandler;
    String mShelf;
    public GatherPresenter() {
        mHandler = new Handler();
        mModel = new GatherModel();
        mModel.setRfidListener(this);
        setDisableShelf(false);
    }

    @Override
    public void onRfid(String epc, String barcode) {
        LogUtils.v("onRfid = " + epc + ",barcode = " + barcode);
        if(mIview == null) {
            return;
        }
        if(RfidUitl.isLevelBarcode(RfidUitl.decodeLevelEpc(epc))) {
            return;
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onScaned(epc,Convert.decryBarcode(barcode));
            }
        });
    }

    public void setDisableShelf(boolean disableShelf) {
        mModel.setDisableShelf(disableShelf);
    }

    @Override
    public void onShelf(String shelf) {
        if(mIview == null) {
            return;
        }
        mShelf = Convert.decryBarcode(shelf);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onShelf(mShelf);
            }
        });
    }

    @Override
    public void onRfid(String rfid) {
    }

    public void gather(List<GatherActivity.Epc> data) {
        for(GatherActivity.Epc epc:data) {
            mModel.gather(mShelf,epc.epc,epc.barcode);
        }
    }

    public void startScanRfid() {
        mModel.startScan();
    }

    public void stopScanRfid() {
        mModel.stopScan();
    }
}
